package business_logic.view;

import java.awt.Color;
import java.awt.Graphics;

import business_logic.Tile;

import fiuba.algo3.titiritero.dibujables.Cuadrado;
import fiuba.algo3.titiritero.dibujables.SuperficiePanel;
import fiuba.algo3.titiritero.modelo.SuperficieDeDibujo;

public class Square extends Cuadrado implements GameView
{
    
    int m_drawingDeadStage;
    int m_size;
    
    public Square( Color color, Observable observable )
    {
        super(Tile.getSize(),Tile.getSize(),observable);
        setColor(color);
        m_size = Tile.getSize();
        m_drawingDeadStage = 0;
    }
    
    @Override
    public void dibujar(SuperficieDeDibujo superfice)
    {
        if ( getObservable().isAlive() )
        {
            super.dibujar(superfice);
        }
        else
        {
            int offset;
            Graphics grafico;
            switch( m_drawingDeadStage )
            {
                case 0:
                    rescale(.5);
                case 1:
                case 2:
                    grafico = ((SuperficiePanel)superfice).getBuffer();
                    grafico.setColor(this.getColor());
                    offset = (int)Tile.getSize()/2-m_size/2;
                    grafico.fillRect( getObservable().getX()+offset, getObservable().getY()+offset, m_size, m_size);
                    m_drawingDeadStage++;
                    break;
                
                case 3:
                    rescale(.5);
                case 4:
                case 5:
                    grafico = ((SuperficiePanel)superfice).getBuffer();
                    grafico.setColor(this.getColor());
                    offset = (int)Tile.getSize()/2-m_size/2;
                    grafico.fillRect( getObservable().getX()+offset, getObservable().getY()+offset, m_size, m_size);
                    m_drawingDeadStage++;
                    break;
                
                case 6:
                    rescale(.5);
                case 7:
                case 8:
                    grafico = ((SuperficiePanel)superfice).getBuffer();
                    grafico.setColor(this.getColor());
                    offset = (int)Tile.getSize()/2-m_size/2;
                    grafico.fillRect( getObservable().getX()+offset, getObservable().getY()+offset, m_size, m_size);
                    m_drawingDeadStage++;
                    break;
                
                case 9:
                    rescale(.5);
                case 10:
                case 11:
                    grafico = ((SuperficiePanel)superfice).getBuffer();
                    grafico.setColor(this.getColor());
                    offset = (int)Tile.getSize()/2-m_size/2;
                    grafico.fillRect( getObservable().getX()+offset, getObservable().getY()+offset, m_size, m_size);
                    m_drawingDeadStage++;
                    break;
                    
                default:
            }
        }
    }

    private void rescale( double scale )
    {
        m_size = (int)(Math.ceil(m_size*scale)+Math.ceil(m_size*scale)%2);
    }
    
    private Observable getObservable()
    {
        return (Observable)getPosicionable();
    }
    
    @Override
    public boolean isDone()
    {
        if ( m_drawingDeadStage>11 && !( getObservable().isAlive() ) )
            return true;
        else
            return false;
    }

    @Override
    public int getZIndex()
    {
        // TODO Auto-generated method stub
        return 0;
    }

}
